package com.example.Cairodio;

import java.util.ArrayList;
import java.util.Vector;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

/**
 * @author Shokry
 * 
 *         This activity is a search-able List View containing all places that
 *         are not included in the tour. The user can is able to choose a place
 *         and add it to his tour. Also the user can filter the list by typing
 *         any substring of the place in the text field.
 * 
 */
public class AddPlaceActivity extends Activity {

	CairodioDbAdapter dbHelper;

	private ListView placesListView;

	// Text field used for filtering the list of places
	private EditText searchText;

	// array_sort is the filtered list of places, according to what the user
	// types in searchText
	private ArrayList<String> array_sort = new ArrayList<String>();

	private Vector<String> placesList;

	// tourPlaces is the list passed by TourActivity which contains all places
	// included in the tour.
	ArrayList<String> tourPlaces;

	/**
	 * @author Shokry
	 * 
	 *         tourPlaces is fetched from the bundle passed from the
	 *         tourAvtivity. The list view is filled with places that do not
	 *         exist in the tour. the UI is initialized
	 * 
	 */
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.add_place);

		tourPlaces = getIntent().getExtras().getStringArrayList(
				getString(R.string.add_place_activity_tour_places));

		// Get Database
		dbHelper = ((CairodioApplication) getApplication()).getAdapter();

		fillPlaces();

		initUI();

	}

	/**
	 * @author Shokry
	 * 
	 *         Initializes the cancel button which navigates back to the Tour
	 *         Activity, title of the view, also sets up search text field which
	 *         listens to the users text change and filters the list of places.
	 *         The button add, navigates back to the tour view and adds the
	 *         chosen place to the tour.
	 */
	private void initUI() {

		Button cancel = (Button) findViewById(R.id.cancel);
		cancel.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {

				finish();
			}
		});

		TextView place = (TextView) findViewById(R.id.Title);
		place.setText(R.string.add_place_activity_title);

		placesListView = (ListView) findViewById(R.id.ListView01);
		placesListView.setAdapter(new ArrayAdapter<String>(this,
				R.layout.add_place_item, R.id.txtItem, placesList));

		searchText = (EditText) findViewById(R.id.EditText01);
		searchText.addTextChangedListener(new TextWatcher() {
			public void afterTextChanged(Editable s) {
				// Abstract Method of TextWatcher Interface.
			}

			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// Abstract Method of TextWatcher Interface.
			}

			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				array_sort.clear();

				// Checks if any place contains the string that the user typed
				for (int i = 0; i < placesList.size(); i++) {
					if (placesList
							.get(i)
							.toLowerCase()
							.indexOf(
									searchText.getText().toString()
											.toLowerCase()) > -1) {
						array_sort.add(placesList.get(i));
					}
				}
				placesListView.setAdapter(new ArrayAdapter<String>(
						AddPlaceActivity.this, R.layout.add_place_item,
						R.id.txtItem, array_sort));
			}
		});

		// Adding a place to the tour
		placesListView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {

				String placeName = (String) ((TextView) (view
						.findViewById(R.id.txtItem))).getText();
				int placeID = dbHelper.getID(placeName);

				// Navigate back to the tour view, returning the id of the
				// chosen pace
				Intent intent = new Intent();
				intent.putExtra(
						getString(R.string.add_place_activity_place_id),
						placeID);
				setResult(RESULT_OK, intent);
				finish();

			}
		});
	}

	/**
	 * @author Shokry
	 * 
	 *         fill places that are not included in the tour. this is done by:
	 *         ListView = AllPlaces - TourPlaces
	 */
	@SuppressWarnings("static-access")
	public void fillPlaces() {

		// Get AllPlaces
		Cursor places = dbHelper.getAllPlaces();
		placesList = new Vector<String>();

		places.moveToFirst();
		while (!places.isAfterLast()) {

			String place = places.getString(places
					.getColumnIndex(dbHelper.colName));

			// Do not add place if it exists in the tour
			if (!tourPlaces.contains(place))
				placesList.add(getValue(place));

			places.moveToNext();
		}

	}
	
	private String getValue(String place) {

		String[] allPlaces = getResources().getStringArray(
				R.array.places_index);

		int index = 0;
		boolean found = false;

		for (; index < allPlaces.length; index++) {
			if (place.equals(allPlaces[index])) {
				found = true;
				break;
			}
		}

		if (!found)
			return place;

		// Translate
		String[] allPlacesNames = getResources().getStringArray(
				R.array.places_values);
		return allPlacesNames[index];

	}
}
